#include "widget.h"
#include "ui_widget.h"
#include "../Common/common.h"

#include <QMouseEvent>
#include <memory>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    setWindowTitle("棋类游戏大厅客户端");
    ui->widget->setFixedHeight(50);

    ui->editIP->setText(QString::fromStdString(serverIp));
    ui->editPort->setText(QString::number(serverPort));        //设置端口
    ui->editRoom->setText(QString::number(defaultRoom));       //设置房间号
    ui->editInput->installEventFilter(this);                   //聊天框安装事件过滤器

    //限制端口编辑框输入格式：0-65535之间的整数
    QIntValidator* pInt = new QIntValidator(0, 65535, this);   //文本验证器
    ui->editPort->setValidator(pInt);

    //房间号可输 0-99
    QIntValidator* pInt2 = new QIntValidator(0, 99, this);
    ui->editRoom->setValidator(pInt2);

    firGameWnd = new FIRGameWnd(0);        //初始化游戏窗口
    ccGameWnd = new CCGameWnd(0);

    connect(firGameWnd, &FIRGameWnd::sigOutput, this, &Widget::slotOutput);                     //游戏窗口的TextEdit输出text信号连接主窗口的槽函数
    connect(firGameWnd, &FIRGameWnd::sigUpdateRoomState, this, &Widget::slotUpdateRoomState);   //游戏窗口的更新房间状态信号触发主窗口的槽函数

    connect(ccGameWnd, &CCGameWnd::sigOutput, this, &Widget::slotOutput);                     //游戏窗口的TextEdit输出text信号连接主窗口的槽函数
    connect(ccGameWnd, &CCGameWnd::sigUpdateRoomState, this, &Widget::slotUpdateRoomState);   //游戏窗口的更新房间状态信号触发主窗口的槽函数
}

Widget::~Widget()
{
    delete firGameWnd;
    delete ccGameWnd;
    firGameWnd = nullptr;
    ccGameWnd = nullptr;
    delete ui;
}

void Widget::on_btnConnect_clicked() //“连接”按钮的槽函数
{
    QString ip = ui->editIP->text();
    int port = ui->editPort->text().toInt();
    int room = ui->editRoom->text().toInt();

    if (room < 50) {
        firGameWnd->Connect(ip, port, room);        //调用游戏窗口的Connect函数，传参主窗口上的ip，端口，房间号
        ui->splitter->insertWidget(0, firGameWnd);
        witchGame = 1;
    }
    else {
        ccGameWnd->Connect(ip, port, room);        //调用游戏窗口的Connect函数，传参主窗口上的ip，端口，房间号
        ui->splitter->insertWidget(0, ccGameWnd);
        witchGame = 2;
    }
}

void Widget::on_btnSend_clicked() //“发送”按钮的槽函数
{
    QString text = ui->editInput->toPlainText();
    if (witchGame == 1) {
        firGameWnd->Say(text);                      //调用游戏窗口的Say函数，传参输入框的text
    }
    else if (witchGame == 2) {
        ccGameWnd->Say(text);
    }
    ui->editInput->clear();
}

void Widget::slotOutput(QString text) //TextEdit输出text
{
    ui->editShow->append(text);
}

void Widget::slotUpdateRoomState() //更新房间状态
{
    QList<ROOM_STATE> rooms;                                  //创建房间状态list
    //调用游戏窗口的获取房间状态函数，传参房间状态列表
    if (witchGame == 1) {
        firGameWnd->GetRoomState(rooms);
    }
    else if (witchGame == 2) {
        ccGameWnd->GetRoomState(rooms);
    }

    ui->listRoom->clear();
    for (int i = 0; i < rooms.size(); i++) {
        ROOM_STATE r = rooms.at(i);                           //房间状态
        QString name1 = QString::fromLocal8Bit(r.szName1);    //客户端1 name
        QString name2 = QString::fromLocal8Bit(r.szName2);    //客户端2 name

        QString text;
        int nPlayerInRoom = r.nPlayers;                       //当前人数 0、1、2
        if (nPlayerInRoom == 0) {                               //根据房间不同人数，设置不同文本
            text = QStringLiteral("房间%1：空闲").arg(i);
        }
        else if (nPlayerInRoom == 1) {
            text = QStringLiteral("房间%1：一位玩家已进入(%2)").arg(i).arg(name1);
        }
        else {
            text = QStringLiteral("房间%1：房间已满！%2、%3").arg(i).arg(name1).arg(name2);
        }

        ui->listRoom->addItem(text);                          //添加一个该文本的房间
    }
}

bool Widget::eventFilter(QObject *target, QEvent *event) //重写事件过滤器
{
    if (target == ui->editInput) {
        if (event->type() == QEvent::KeyPress) {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if (keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return) {
                on_btnSend_clicked();
                return true;
            }
        }
    }
    return QWidget::eventFilter(target, event);
}




